//
//  CXPayTypeCollectionView.m

//

#import "CXPayTypeCollectionView.h"
#import "CXPayTypeCollectionCell.h"
static NSString *const PayTypeCollectionCell = @"CXPayTypeCollectionCell";
@interface CXPayTypeCollectionView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@end
@implementation CXPayTypeCollectionView


+(instancetype)loadPayTypeCollectionView
{
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:self options:nil].firstObject;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((HNTScreenWidth-51)/4.0, (HNTScreenWidth-51)/4.0);
    
    //在垂直方向  设置的是cell上下之间的最小间距 在水平方向  设置的是cell左右之间的最小间距
    layout.minimumLineSpacing = 10.0 ;
    //在垂直方向  设置的是cell左右之间的最小间距 在水平方向  设置的是cell上下之间的最小间距
    layout.minimumInteritemSpacing = 10.0;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CXPayTypeCollectionCell class]) bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:PayTypeCollectionCell];
    _collectionView.collectionViewLayout = layout;
    _collectionView.backgroundColor = KWhiteColor;
    _collectionView.contentInset = UIEdgeInsetsMake(0, 10, 0, 10);
}

-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [_collectionView reloadData];
}

-(void)setPayMount:(NSString *)payMount
{
    _payMount = payMount;
    _priceLabel.text = [NSString stringWithFormat:@"￥%g",[_payMount floatValue]];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXPayTypeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PayTypeCollectionCell forIndexPath:indexPath];
    
    if (cell == nil)
    {
        cell = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([cell class]) owner:self options:nil].firstObject;
    }
    
    if (_dataArray.count)
    {
        cell.model = _dataArray[indexPath.row];
    }
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXPayTypeModel *selectModel = _dataArray[indexPath.row];
    for (CXPayTypeModel *subModel in _dataArray)
    {
        if ([subModel.pay_id isEqualToString:selectModel.pay_id])
        {
            subModel.isSelect = @"1";
        }
        else
            subModel.isSelect = @"0";
    }
    [collectionView reloadData];
}

- (IBAction)cancleClick:(UIButton *)sender
{
    if(self.delegate &&[self.delegate respondsToSelector:@selector(view:WithIsConfirm:andModel:)])
    {
        if (sender.tag == 1)
        {
            BOOL isSelect = NO;
            for (int i = 0; i<_dataArray.count; i++)
            {
                CXPayTypeModel *model = _dataArray[i];
                if ([model.isSelect isEqualToString:@"1"])
                {
                    [self.delegate view:self WithIsConfirm:sender.tag andModel:model];
                    isSelect = YES;
                }
            }
            if (isSelect == NO)
            {
                [MBManager showBriefAlert:@"请选择支付方式"];
            }
        }
        else
        {
            [self.delegate view:self WithIsConfirm:sender.tag andModel:nil];
        }
    }
}


@end
